/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Endlesslove
 */
public class ChiTietBaoCaoTon implements IDaTa{

    @Override
    public ResultSet Search(DTO.Idata Obj)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Update(DTO.Idata Obj) 
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Delete(DTO.Idata Obj) 
    {
         List<String> params = new ArrayList<>();
        params.add("char:" + ((DTO.ChiTietBaoCaoTon)Obj).getID());
        return DAO.CallProc("XoaChiTietBaoCaoTon", params, true);
    }

    @Override
    public void LoadData(Object table) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean Insert(DTO.Idata Obj) 
    {
        List<String> params = new ArrayList<>();
        params.add("char:" +  ((DTO.ChiTietBaoCaoTon)Obj).getID()) ;//ma Chi Tiet BCT
        params.add("char:" +  ((DTO.ChiTietBaoCaoTon)Obj).MaBaoCaoTon);//Ma Bao Cao Ton
        params.add("char:" +  ((DTO.ChiTietBaoCaoTon)Obj).MaSach);//Ma Sach
        params.add("smallint:" +  ((DTO.ChiTietBaoCaoTon)Obj).TonDau);//Ton Dau
        params.add("smallint:" + ((DTO.ChiTietBaoCaoTon)Obj).PhatSinh);//Phat Sinh
        params.add("smallint:" +  ((DTO.ChiTietBaoCaoTon)Obj).TonCuoi );//Ton Cuoi
        return DAO.CallProc("ThemChiTietBaoCaoTon", params, true);
    }

    @Override
    public String GenerateID()
    {
        try 
        {
            ResultSet res = DAO.CallProc("LayMaChiTietBaoCaoTonCuoi");
            if (res.next())
            {    
              int index  = Integer.parseInt(((String)res.getObject(1)).split("CTBCT")[1].trim());
              String ms = "CTBCT";
              ms += (index+1)>=10?(index+1)>=100? (index +1)>=1000?(index+1) : "0" + (index +1) : "00" + (index + 1):"000"+(index+1);
              return ms;
            }
           return "CTBCT001";
        } 
        catch (SQLException ex) 
        {
            return "CTBCT001";
        }
        catch(Exception ie)
        {
            return "CTBCT001";
        }
    }
    
}
